package haco.syncremotefile.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author wangyuwei
 * @history 2021-06-02 16:13
 */
@Slf4j
@Service
public class Upload2oss {

    @Value("${hg.oss.endpoint}")
    String endpoint ;
    @Value("${hg.oss.accessKeyId}")
    String accessKeyId ;
    @Value("${hg.oss.accessKeySecret}")
    String accessKeySecret ;
    @Value("${hg.oss.bucketName}")
    String bucketName;

    public  OSS getConnectOss(){
         // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        log.info("已连接oss");
        return ossClient ;
    }

    public String getBucketName(){
        return this.bucketName;
    }

    public void closeOss(OSS ossClient){
        try {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
